package com.irdstudio.apicenter.console.controller.authenticate;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.irdstudio.apicenter.console.controller.MacmsBaseAction;
import com.irdstudio.apicenter.console.dm.AppCommodityShow;
import com.irdstudio.apicenter.console.service.authenticate.AppCommodityShowService;


@Controller
@RequestMapping(value = "/appCommodityShow")
public class AppCommodityShowAction extends MacmsBaseAction {
	@Autowired
	private AppCommodityShowService appCommodityShowService;

	// 插入跳转
	@RequestMapping(value = "jumpAddCommodityShow")
	public String jumpAddCommodityShow(HttpServletRequest request) {
	    String appid = request.getParameter("appid");
		request.setAttribute("appid", appid);
		return "auth/addCommodityShow";
	}

	// 插入保存
	/*@RequestMapping(value = "addCommodityShow")
	public void addCommodityShow(@ModelAttribute("appCommodityShow") AppCommodityShow appCommodityShow,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		flag = appCommodityShowService.insertOne(appCommodityShow);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}*/
	@RequestMapping(value = "addCommodityShow")
	public @ResponseBody
	ModelAndView addCommodityShow(
			@ModelAttribute("appCommodityShow") AppCommodityShow appCommodityShow)
			throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		int flag = -1;
		System.out.println(appCommodityShow.getApp_id()+"==="+appCommodityShow.getCommodity_name()+"==="+appCommodityShow.getCommodity_file_path()+"==="+appCommodityShow.getLong_desc());
		flag = appCommodityShowService.insertOne(appCommodityShow);
		System.out.println("插入条数：flag="+flag);
		modelAndView.addObject("flag", flag);
		return modelAndView;
	}

	// 更新跳转
	@RequestMapping(value = "jumpUpdateCommodityShow")
	public String jumpUpdateCommodityShow(HttpServletRequest request)
			throws SQLException, ClassNotFoundException {
		String recordKey = request.getParameter("recordKey");
		String appid = request.getParameter("appid");
		AppCommodityShow appCommodityShow = appCommodityShowService.queryAppTradePrivate(recordKey, appid);
		request.setAttribute("appCommodityShow", appCommodityShow);
		request.setAttribute("appid", appid);
		return "auth/updateCommodityShow";
	}

	// 更新保存
	/*@RequestMapping(value = "updateCommodityShow")
	public void updateCommodityShow(@ModelAttribute("appCommodityShow") AppCommodityShow appCommodityShow,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		flag = appCommodityShowService.update(appCommodityShow);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}*/
	@RequestMapping(value = "updateCommodityShow")
	public @ResponseBody
	ModelAndView updateCommodityShow(
			@ModelAttribute("appCommodityShow") AppCommodityShow appCommodityShow)
			throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		int flag = -1;
		System.out.println(appCommodityShow.getApp_id()+"==="+appCommodityShow.getCommodity_name()+"==="+appCommodityShow.getCommodity_file_path()+"==="+appCommodityShow.getLong_desc());
		flag = appCommodityShowService.update(appCommodityShow);
		System.out.println("插入条数：flag="+flag);
		modelAndView.addObject("flag", flag);
		return modelAndView;
	}

	// 删除
	@RequestMapping(value = "deleteCommodityShow")
	public void deleteCommodityShow(HttpServletRequest request,
			HttpServletResponse response) throws SQLException,
			ClassNotFoundException {
		int flag = -1;
		String recordKey = request.getParameter("recordKey");
		String appid = request.getParameter("appid");
		flag = appCommodityShowService.delete(appid, recordKey);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	// 查看
	@RequestMapping(value = "queryCommodityShowDetail")
	public String queryCommodityShowDetail(HttpServletRequest request) throws SQLException,
			ClassNotFoundException {
		String recordKey = request.getParameter("recordKey");
		String appid = request.getParameter("appid");
		AppCommodityShow appCommodityShow = appCommodityShowService.queryAppTradePrivate(recordKey, appid);
		request.setAttribute("appCommodityShow", appCommodityShow);
		request.setAttribute("appid", appid);
		return "auth/queryCommodityShowDetail";
	}

}

